package de.grosjohann.android.feuerwehr;

import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.FMSNotificationService;

public class StatusActivity
  extends Activity {

  private Fahrzeug fahrzeug;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.status);
    setTitle(R.string.lblStatusChange);

    StatusButtonListener statusButtonListener = new StatusButtonListener();

    Button btnStatus1 = (Button) findViewById(R.id.btnStatus1);
    btnStatus1.setOnClickListener(statusButtonListener);

    Button btnStatus2 = (Button) findViewById(R.id.btnStatus2);
    btnStatus2.setOnClickListener(statusButtonListener);

    Button btnStatus3 = (Button) findViewById(R.id.btnStatus3);
    btnStatus3.setOnClickListener(statusButtonListener);

    Button btnStatus4 = (Button) findViewById(R.id.btnStatus4);
    btnStatus4.setOnClickListener(statusButtonListener);

    Button btnStatus5 = (Button) findViewById(R.id.btnStatus5);
    btnStatus5.setOnClickListener(statusButtonListener);

    Button btnStatus6 = (Button) findViewById(R.id.btnStatus6);
    btnStatus6.setOnClickListener(statusButtonListener);

    Button btnStatus0 = (Button) findViewById(R.id.btnStatus0);
    btnStatus0.setOnClickListener(statusButtonListener);

    Button button = (Button) findViewById(R.id.btnStatusCancel);
    button.setOnClickListener(new CancelButtonListener());

    FMSNotificationService.updateUIListener = new IUpdateUIListener() {

      @Override
      public void updateUI(String data) {
        // nothing to do      
      }

      @Override
      public void showInfo(String info) {
        finish();
      }

      @Override
      public void showWarning(String warning) {
        // nothing to do        
      }

      @Override
      public void showError(final String error) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), StatusActivity.this);
            panel.showError(R.string.errSendFMSData);
          }
        });
      }

    };
  }

  @Override
  public void onResume() {
    super.onResume();

    Bundle b = getIntent().getExtras();
    if (b != null) {
      Serializable serObj = b.getSerializable(FMSActivity.FMS_OBJECT);
      if (serObj instanceof Fahrzeug) {
        fahrzeug = (Fahrzeug) serObj;

        TextView tvDescription = (TextView) findViewById(R.id.tvStatusDescription);
        tvDescription.setText(fahrzeug.getDescription());
        TextView tvKennung = (TextView) findViewById(R.id.tvStatusKennung);
        tvKennung.setText(fahrzeug.getIdentifier());
      }
    }
  }

  @Override
  protected void onStop() {
    super.onStop();
    fahrzeug = null;
  }

  private class StatusButtonListener
    implements View.OnClickListener {

    @Override
    public void onClick(View newView) {

      if (newView instanceof Button) {
        Button btnStatus = (Button) newView;
        String status = (String) btnStatus.getText();

        Fahrzeug f = StatusActivity.this.fahrzeug;

        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        Intent intent = new Intent(StatusActivity.this, FMSNotificationService.class);
        intent.putExtra(FMSNotificationService.HTTP_METHOD, FMSNotificationService.HTTP_METHOD_POST);
        intent.putExtra(FMSNotificationService.FMS_STATUS_AUTHORIZATION, tm.getDeviceId());
        intent.putExtra(FMSNotificationService.FMS_STATUS_KENNUNG, f.getIdentifier());
        intent.putExtra(FMSNotificationService.FMS_STATUS_DATA, status);
        startService(intent);
      }

    }
  }

  private class CancelButtonListener
    implements View.OnClickListener {

    @Override
    public void onClick(View newV) {
      finish();
    }
  }
}